﻿using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Telerik.Web.Mvc;
using viBook.Common;
using viBook.Filters;
using viBook.Models;
using viBook.Repositories;

namespace viBook.Controllers
{
    [ExtendedAuthentication]
    public class AdminsController : BaseController
    {
        #region Constructors and Repository vars

        private readonly IAuthorRepository _authorRepository;
        private readonly ICategoryRepository _categoryRepository;
        private readonly IContentTagRepository _contentTagRepository;
        private readonly IReportRepository _reportRepository;
        private readonly IUserRepository _userRepository;
        private readonly IChapterCommentRepository _chapterCommentRepository;
        private readonly IBookCommentRepository _bookCommentRepository;

        public AdminsController(ICategoryRepository categoryRepository,
                                IContentTagRepository contentTagRepository,
                                IAuthorRepository authorRepository,
                                IUserRepository userRepository,
                                IReportRepository reportRepository,
                                IChapterCommentRepository chapterCommentRepository,
                                IBookCommentRepository bookCommentRepository)
        {
            _categoryRepository = categoryRepository;
            _contentTagRepository = contentTagRepository;
            _authorRepository = authorRepository;
            _userRepository = userRepository;
            _reportRepository = reportRepository;
            _chapterCommentRepository = chapterCommentRepository;
            _bookCommentRepository = bookCommentRepository;
        }

        #endregion

        public ActionResult Index()
        {
            return View();
        }

        #region Categories

        public ActionResult Categories()
        {
            return View(_categoryRepository.All);
        }

        [GridAction]
        public ActionResult SelectCategoryAjax()
        {

            var categories = new List<Category>();
            foreach (var category in _categoryRepository.All)
            {
                categories.Add(new Category
                {
                    Id = category.Id,
                    CategoryName = category.CategoryName,
                    NumberOfBook = category.NumberOfBook,
                });
            }
            return View(new GridModel(categories));
        }

        [GridAction]
        public ActionResult DeleteCategoryAjax(int id)
        {
            var result = _categoryRepository.Delete(id);
            if (!result)
            {
                Response.StatusCode = 500;
                return Content(Constant.CategoryContainsBooks);
            }
            return SelectCategoryAjax();
        }

        [GridAction]
        public ActionResult InsertCategoryAjax(Category category)
        {
            if (ModelState.IsValid)
            {
                _categoryRepository.InsertOrUpdate(category);
            }
            return SelectCategoryAjax();
        }

        [GridAction]
        public ActionResult UpdateCategoryAjax(Category category)
        {
            if (ModelState.IsValid)
            {
                _categoryRepository.InsertOrUpdate(category);
            }
            return SelectCategoryAjax();
        }

        #endregion

        #region ContentTags

        public ActionResult ContentTags()
        {
            return View(_contentTagRepository.All);
        }

        [GridAction]
        public ActionResult SelectContentTagAjax()
        {
            return View(new GridModel(_contentTagRepository.All));
        }

        [GridAction]
        public ActionResult DeleteContentTagAjax(int id)
        {
            var result = _contentTagRepository.Delete(id);
            if (!result)
            {
                Response.StatusCode = 500;
                return Content(Constant.ContentTagContainsBooks);
            }
            return SelectContentTagAjax();
        }

        [GridAction]
        public ActionResult InsertContentTagAjax(ContentTag contentTag)
        {
            if (ModelState.IsValid)
            {
                if (!string.IsNullOrEmpty(contentTag.ContentTagName))
                    contentTag.ContentTagName = contentTag.ContentTagName.Replace(';', ' ').Trim(' ');
                _contentTagRepository.Insert(new List<ContentTag> { contentTag });
            }
            return SelectContentTagAjax();
        }

        [GridAction]
        public ActionResult UpdateContentTagAjax(ContentTag contentTag)
        {
            if (ModelState.IsValid)
            {
                _contentTagRepository.Update(contentTag);
            }
            return SelectContentTagAjax();
        }

        #endregion

        #region Authors

        public ActionResult Authors()
        {
            return View(_authorRepository.All);
        }

        [GridAction]
        public ActionResult SelectAuthorAjax()
        {
            return View(new GridModel(_authorRepository.All));
        }

        [GridAction]
        public ActionResult DeleteAuthorAjax(int id)
        {
            _authorRepository.Delete(id);
            return SelectAuthorAjax();
        }

        [GridAction]
        public ActionResult InsertAuthorAjax(Author author)
        {
            if (ModelState.IsValid)
            {
                _authorRepository.InsertOrUpdate(author);
            }
            return SelectAuthorAjax();
        }

        [GridAction]
        public ActionResult UpdateAuthorAjax(Author author)
        {
            if (ModelState.IsValid)
            {
                _authorRepository.InsertOrUpdate(author);
            }
            return SelectAuthorAjax();
        }

        #endregion

        #region Users

        public ActionResult Users()
        {
            return View(_userRepository.All);
        }

        [GridAction]
        public ActionResult SelectUserAjax()
        {
            var resultList = new List<User>();
            var usersList = _userRepository.All;
            foreach (var user in usersList)
            {
                var u = new User
                {
                    Id = user.Id,
                    AvatarUrl = user.AvatarUrl,
                    Biography = user.Biography,
                    DisplayName = user.DisplayName,
                    Email = user.Email,
                    IsAdmin = user.IsAdmin,
                    IsBlocked = user.IsBlocked,
                    IsExternal = user.IsExternal,
                    JoinedDate = user.JoinedDate,
                    LastJoined = user.LastJoined,
                    Status = user.Status,
                    Location = user.Location,
                    Username = user.Username,
                    Password = user.Password
                };
                resultList.Add(u);
            }
            return View(new GridModel(resultList));
        }

        [GridAction]
        public ActionResult DeleteUserAjax(string username)
        {
            _userRepository.Delete(username);
            return SelectUserAjax();
        }

        [GridAction]
        public ActionResult BlockUser(int id)
        {
            _userRepository.BlockUser(id);
            return SelectUserAjax();
        }

        [GridAction]
        public ActionResult UnblockUser(int id)
        {
            _userRepository.UnblockUser(id);
            return SelectUserAjax();
        }

        #endregion

        #region Chapter Comment Report

        public ActionResult ChapterCommentsReport()
        {
            var reportsList = new List<ReportOnChapterComment>();
            var reportsOnChapterComment = _reportRepository.AllReportOnChapterComment;
            foreach (var report in reportsOnChapterComment)
            {
                var chapterComment = _chapterCommentRepository.Find(report.ChapterCommentId);
                report.ChapterComment = new ChapterComment
                {
                    Id = chapterComment.Id,
                    ChapterId = chapterComment.ChapterId,
                    Content = chapterComment.Content
                };
                reportsList.Add(report);
            }
            return View(reportsList);
        }

        [GridAction]
        public ActionResult SelectChapterCommentReportAjax()
        {
            var reportsList = new List<ReportOnChapterComment>();
            var reportsOnChapterComment = _reportRepository.AllReportOnChapterComment;
            foreach (var report in reportsOnChapterComment)
            {
                var chapterComment = _chapterCommentRepository.Find(report.ChapterCommentId);
                var res = new ReportOnChapterComment
                {
                    Id = report.Id,
                    UserId = report.UserId,
                    ReportedDate = report.ReportedDate,
                    IsApproved = report.IsApproved,
                    ChapterCommentId = report.ChapterCommentId,
                    ChapterComment = new ChapterComment
                    {
                        Id = chapterComment.Id,
                        ChapterId = chapterComment.ChapterId,
                        Content = chapterComment.Content
                    }
                };
                reportsList.Add(res);
            }
            return View(new GridModel(reportsList));
        }

        [GridAction]
        public ActionResult DisapproveChapterCommentReportAjax(int id)
        {
            _reportRepository.DisapproveReportOnChapterComment(id);
            return SelectChapterCommentReportAjax();
        }

        [GridAction]
        public ActionResult ApproveChapterCommentReportAjax(int id)
        {
            _reportRepository.ApproveReportOnChapterComment(id);
            return SelectChapterCommentReportAjax();
        }

        #endregion

        #region Book Comment Report

        public ActionResult BookCommentsReport()
        {
            var reportsList = new List<ReportOnBookComment>();
            var reportsOnBookComment = _reportRepository.AllReportOnBookComment;
            foreach (var report in reportsOnBookComment)
            {
                var bookComment = _bookCommentRepository.Find(report.BookCommentId);
                report.BookComment = new BookComment
                {
                    Id = bookComment.Id,
                    BookId = bookComment.BookId,
                    Content = bookComment.Content
                };
                reportsList.Add(report);
            }
            return View(reportsList);
        }

        [GridAction]
        public ActionResult SelectBookCommentReportAjax()
        {
            var reportsList = new List<ReportOnBookComment>();
            var reportsOnBookComment = _reportRepository.AllReportOnBookComment;
            foreach (var report in reportsOnBookComment)
            {
                var bookComment = _bookCommentRepository.Find(report.BookCommentId);
                var res = new ReportOnBookComment
                {
                    Id = report.Id,
                    UserId = report.UserId,
                    ReportedDate = report.ReportedDate,
                    IsApproved = report.IsApproved,
                    BookCommentId = report.BookCommentId,
                    BookComment = new BookComment
                    {
                        Id = bookComment.Id,
                        BookId = bookComment.BookId,
                        Content = bookComment.Content
                    }
                };
                reportsList.Add(res);
            }
            return View(new GridModel(reportsList));
        }

        [GridAction]
        public ActionResult DisapproveBookCommentReportAjax(int id)
        {
            _reportRepository.DisapproveReportOnBookComment(id);
            return SelectBookCommentReportAjax();
        }

        [GridAction]
        public ActionResult ApproveBookCommentReportAjax(int id)
        {
            _reportRepository.ApproveReportOnBookComment(id);
            return SelectChapterCommentReportAjax();
        }

        #endregion

        #region Books Report

        public ActionResult BooksReport()
        {
            var reportsList = new List<ReportOnBook>();
            var reportsOnBook = _reportRepository.AllReportOnBook;
            foreach (var report in reportsOnBook)
            {
                var res = new ReportOnBook
                {
                    Id = report.Id,
                    UserId = report.UserId,
                    ReportedDate = report.ReportedDate,
                    IsApproved = report.IsApproved,
                    BookId = report.BookId,
                    Book = report.Book
                };
                reportsList.Add(res);
            }
            return View(reportsList);
        }

        [GridAction]
        public ActionResult SelectBookReportAjax()
        {
            var reportsList = new List<ReportOnBook>();
            var reportsOnBook = _reportRepository.AllReportOnBook;
            foreach (var report in reportsOnBook)
            {
                var res = new ReportOnBook
                {
                    Id = report.Id,
                    UserId = report.UserId,
                    ReportedDate = report.ReportedDate,
                    IsApproved = report.IsApproved,
                    BookId = report.BookId,
                    Book = report.Book
                };
                reportsList.Add(res);
            }
            return View(new GridModel(reportsList));
        }

        [GridAction]
        public ActionResult DisapproveBookReportAjax(int id)
        {
            _reportRepository.DisapproveReportOnBook(id);
            return SelectBookReportAjax();
        }

        [GridAction]
        public ActionResult ApproveBookReportAjax(int id)
        {
            _reportRepository.ApproveReportOnBook(id);
            return SelectBookReportAjax();
        }

        #endregion

        #region Users Report

        public ActionResult UsersReport()
        {
            var reportsList = new List<ReportOnUser>();
            var reportsOnUser = _reportRepository.AllReportOnUser;
            foreach (var report in reportsOnUser)
            {
                var res = new ReportOnUser
                {
                    Id = report.Id,
                    UserId = report.UserId,
                    ReportedDate = report.ReportedDate,
                    IsApproved = report.IsApproved,
                    ReporteeId = report.ReporteeId,
                    Reportee = new User
                    {
                        Id = report.ReporteeId,
                        Email = report.Reportee.Email,
                        DisplayName = report.Reportee.DisplayName,
                        IsBlocked = report.Reportee.IsBlocked,
                        Username = report.Reportee.Username
                    }
                };
                reportsList.Add(res);
            }
            return View(reportsList);
        }

        [GridAction]
        public ActionResult SelectUserReportAjax()
        {
            var reportsList = new List<ReportOnUser>();
            var reportsOnBook = _reportRepository.AllReportOnUser;
            foreach (var report in reportsOnBook)
            {
                var res = new ReportOnUser
                {
                    Id = report.Id,
                    UserId = report.UserId,
                    ReportedDate = report.ReportedDate,
                    IsApproved = report.IsApproved,
                    ReporteeId = report.ReporteeId,
                    Reportee = report.Reportee
                };
                reportsList.Add(res);
            }
            return View(new GridModel(reportsList));
        }

        [GridAction]
        public ActionResult DisapproveUserReportAjax(int id)
        {
            _reportRepository.DisapproveReportOnUser(id);
            return SelectUserReportAjax();
        }

        [GridAction]
        public ActionResult ApproveUserReportAjax(int id)
        {
            _reportRepository.ApproveReportOnUser(id);
            return SelectUserReportAjax();
        }

        #endregion
    }
}